
/**
 * form表格
 */
var FormTable = {
	tableId:'',
	ftSign:'',//表单标识
	resType:null,//表单类型
	arrayFmt:null,//排列格式
	fields:null,//form表单元素
	dicsMap:null,//form表单元素字典
	data:null,//form表单元素数据
	
	/**
	 * 创建form表单html
	 */
	createHtml:function(){
		FormTable.toMapField();
		if(FormTable.resType==1){//普通表单
			var data = FormTable.fmtField(FormTable.arrayFmt);
			var maxCol = data[0];
			var fieldRows = data[1];
			return FormTable.createFmtFormTable(maxCol,fieldRows);
		}else if(FormTable.resType==2){//列表表单
			var listHtml = "<table width='100%' id='"+FormTable.tableId+"'>";
			listHtml += "</table>";
			return listHtml;
//			return FormTable.createListTable();
		}
	},
	/**
	 * form表单元素转换Map格式
	 */
	toMapField:function(){
		var fieldMap = {};
		$.each(FormTable.fields,function(i,field){
			var tagName = field.tagName;
			fieldMap[tagName] = field;
		});
		FormTable.fieldMap = fieldMap;
	},
	/**
	 * 排列表单元素
	 * @param arrayFmt
	 */
	fmtField:function(arrayFmt){
		var maxCol = 1;
		var fieldRows = null;
		if(arrayFmt){
			fieldRows = arrayFmt.split(";");
			for(var i=0;i<fieldRows.length;i++){
				var fieldRow = fieldRows[i];
				fieldRow = fieldRow.replace(/ /g,"");
				if(fieldRow!=""){
					var fieldCols = fieldRow.split(",");
					if(fieldCols.length>maxCol){
						maxCol = fieldCols.length;
					}
					fieldRows[i] = fieldCols;
				}
			}
		}else{
			maxCol = 1;
			fieldRows = new Array(FormTable.fields.length);
			$.each(FormTable.fields,function(i,field){
				fieldRows[i] = new Array(field.tagName);
			});
		}
		var data = new Array(2);
		data[0] = maxCol;
		data[1] = fieldRows;
		return data;
	},
	/**
	 * 创建格式表单
	 * @param maxCol
	 * @param fieldRows
	 */
	createFmtFormTable:function(maxCol,fieldRows){
		var tableClass = "class='formTable' width='100%'";
		var textClass = "class='formTdText'";
		var inputClass = "class='formTdInput'";
		if(FormTable.ftSign=='cnd'){
			tableClass = "";
			textClass = "";
			inputClass = "";
		}
		var formTable = "<table "+tableClass+">";
		for(var i=0;i<fieldRows.length;i++){
			var fieldCols = fieldRows[i];
			var cellIndex = 0;
			formTable += "<tr>";
			for(var j=0;j<fieldCols.length;j++){
				var fieldName = fieldCols[j];
				var field = FormTable.fieldMap[fieldName];
				var inputHtml = FormTable.parseFieldHtml(field);
				var colspan = 1;
				if (j == fieldCols.length - 1){
					colspan = maxCol * 2 - cellIndex - 1;
				}
				var colspanHtml = "";
				if (colspan > 1){
					colspanHtml = " colspan='" + colspan + "'";
				}
				formTable += "<td "+textClass+">"+field.tagTitle+"：</td>";
				formTable += "<td "+inputClass+" "+colspanHtml+">"+inputHtml+"</td>";
				cellIndex = cellIndex + 1 + colspan;
			}
			formTable += "</tr>";
		}
		formTable += "</table>";
		return formTable;
	},
	/**
	 * 创建普通表单表格
	 * @returns {String}
	 */
	createFormTable:function(){
		var formTable = "<table class='formTable' width='100%'>";
		$.each(FormTable.fieldMap,function(fieldName,field){
			var inputHtml = FormTable.parseFieldHtml(field);
			formTable += "<tr>";
			formTable += "<td class='formTdText'>"+field.tagTitle+"：</td>";
			formTable += "<td class='formTdInput'>"+inputHtml+"</td>";
			formTable += "</tr>";
		});
		formTable += "</table>";
		return formTable;
	},
	/**
	 * 创建列表表单
	 * @returns {String}
	 */
	createListTable:function(){
		var operHtml = FormTable.createListOper();
		$("#"+FormTable.tableId).after(operHtml);
		var fieldNamePre = "";
		if(FormTable.ftSign){
			fieldNamePre = FormTable.ftSign+"_";
		}
		var columns = new Array();
		var data = new Array();
		columns.push({field:'del',title:'删除'})
		var fieldData = {};
		var operHtml = "<a href=\"javascript:FormTable.delRow('"+FormTable.tableId+"',0)\">删除</a>";
		fieldData['del'] = operHtml;
		$.each(FormTable.fields,function(i,field){
			var fieldName = fieldNamePre+field.tagName;
			var fieldColumn = {};
			fieldColumn["field"] = fieldName;
			fieldColumn["title"] = field.tagTitle;
			fieldColumn["editor"] = {type:'text',options:{required:true}};
			columns.push(fieldColumn);
			var inputHtml = FormTable.parseFieldHtml(field);
			fieldData[fieldName] = inputHtml;
		});
		data.push(fieldData);
		$("#"+FormTable.tableId).datagrid({
			fitColumns:true,
			singleSelect:true,
			columns:[columns],
			data:data
		});
//		FormTable.insertRow(FormTable.tableId);
		/*
		var listHtml = "<table class='easyui-datagrid' data-options='fitColumns:true' width='100%' id='"+FormTable.tableId+"'>";
		listHtml += "<thead><tr>";
		listHtml += "<th data-options=\"field:'del',fixed:true\">删除</th>";
		var fieldNamePre = "";
		if(FormTable.ftSign){
			fieldNamePre = FormTable.ftSign+"_";
		}
		$.each(FormTable.fields,function(i,field){
			var fieldName = fieldNamePre+field.tagName;
			listHtml += "<th data-options=\"field:'"+fieldName+"',fixed:true\">"+field.tagTitle+"</th>";
		});
		listHtml += "</tr></thead>";
		listHtml += "<tr>";
		listHtml += "<td><a href=\"javascript:FormTable.delRow('"+FormTable.tableId+"',0)\">删除</a></td>";
		$.each(FormTable.fields,function(i,field){
			var inputHtml = FormTable.parseFieldHtml(field);
			listHtml += "<td>"+inputHtml+"</td>";
		});
		listHtml += "</tr>";
		listHtml += "</table>";
		var operHtml = FormTable.createListOper();
		listHtml += operHtml;
		*/
//		return listHtml;
	},
	createListOper:function(){
		var listOperHtml = "<table width='100%'>";
		listOperHtml += "<tr>";
		listOperHtml += "<td>";
		var addRowButton = "<a class=\"easyui-linkbutton\" href=\"javascript:FormTable.insertRow('"+FormTable.tableId+"');\">添加</a>";
		listOperHtml += addRowButton;
		listOperHtml += "</td>";
		listOperHtml += "</tr>";
		listOperHtml += "</table>";
		return listOperHtml;
	},
	insertRow:function(batchTblId){
		var fieldNamePre = "";
		if(FormTable.ftSign){
			fieldNamePre = FormTable.ftSign+"_";
		}
		var row = {};
		var listRow = $('#'+batchTblId).datagrid('getRows');
		var size = listRow.length;
		row['del'] = "<a href=\"javascript:FormTable.delRow('"+FormTable.tableId+"',"+size+");\">删除</a>";
		$.each(FormTable.fields,function(i,field){
			var fieldName = fieldNamePre+field.tagName;
			var inputHtml = FormTable.parseFieldHtml(field);
			row[fieldName] = inputHtml;
		});
		$('#'+batchTblId).datagrid('insertRow',{index:size+1,row:row});
		FormTable.validateForm();
//		var opts = $('#'+batchTblId).datagrid('getColumnFields');
//		var colp = $('#'+batchTblId).datagrid('getColumnOption','dic_col_DICID');
//		console.info(colp);
	},
	delRow:function(batchTblId,index){
		$('#'+batchTblId).datagrid('deleteRow',index);
	},
	/**
	 * 生成表单元素html
	 * @param field
	 * @returns {String}
	 */
	parseFieldHtml:function(field){
		var inputStr = "";
		var fieldName = field.tagName;
		var width = 20;
		if(field.tagLen){
			width = field.tagLen;
		}
		var value = "";
		if(FormTable.data){
			value = FormTable.data[fieldName];
		}else if(field.initValue){
			value = field.initValue;
		}
		if(!ToolsUtil.isNotNull(value)){
			value = "";
		}
		
		var fieldNamePre = "";
		if(FormTable.ftSign){
			fieldNamePre = FormTable.ftSign+"_";
		}
		if(field.tagType==7){//单行输入框
			inputStr = "<input type='text' size='"+width+"' name='"+fieldNamePre+fieldName+"' value='"+value+"'/>";
		}else if(field.tagType==8){//多行输入框
			inputStr = "<textarea rows='3' cols='50' name="+fieldNamePre+fieldName+">"+value+"</textarea>";
		}else if(field.tagType==11){//可编辑弹出字典
			inputStr = "<input type='text' size='"+width+"' name='"+fieldNamePre+fieldName+"' id='"+fieldNamePre+fieldName+"' value='"+value+"'/>";
			inputStr += "<a href='javascript:void(0)' onclick=\"javascript:System.popSetDic('"+field.dicSubSys+"',"+field.dicKind+","+field.dicGrpid+","+field.valDicCol+",'"+fieldNamePre+fieldName+"');\">选择</a>";
		}else if(field.tagType==5){//密文
			inputStr = "<input type='password' size='"+width+"' name='"+fieldNamePre+fieldName+"' id='"+Form.fieldNamePre+fieldName+"' value='"+value+"'/>";
		}else if(field.tagType==2){//下拉框
			inputStr = "<select id='"+fieldNamePre+fieldName+"' name='"+fieldNamePre+fieldName+"' style='min-width: 120px;'>";
			inputStr += "<option value=''>--请选择--</option>";
			if((field.dicKind==1||field.dicKind==4)&&FormTable.dicsMap){
				var dicKey = field.dicKind +"_"+field.dicGrpid;
				var dics = FormTable.dicsMap[dicKey];
				if(dics){
					$.each(dics,function(i,dic){
						var sel = FormTable.data&&FormTable.data[fieldName]==dic.key ? 'selected="selected"' : '';
						var dicKey = dic.key;
						if(field.valDicCol==2){
							dicKey = dic.value;
						}
						inputStr += "<option value='"+dicKey+"' "+sel+">"+dic.value+"</option>";
					});
				}
			}
			inputStr += "</select>";
		}else if(field.tagType==-1||field.tagType==-4){
			inputStr = value;
			inputStr += "<input type='hidden' name='"+fieldNamePre+fieldName+"' value='"+value+"'/>";
		}
		if(field.tagType!=-1&&field.postFixTag){
			inputStr = inputStr + field.postFixTag;
		}
		return inputStr;
	},
	validateForm:function(){
		var fieldNamePre = "";
		if(FormTable.ftSign){
			fieldNamePre = FormTable.ftSign+"_";
		}
		$.each(FormTable.fieldMap,function(fieldName,field){
			var validOption = {};
			if(!field.nullAble){
				validOption.required = true;
				validOption.missingMessage = field.tagTitle+'不能为空';
			}
			if(field.tagType==7){
				$("input[name='"+fieldNamePre+field.tagName+"']").validatebox(validOption);
			}else if(field.tagType==8){
				validOption.multiline = true;
				validOption.height = 60;
				$("textarea[name='"+fieldNamePre+field.tagName+"']").textbox(validOption);
			}else if(field.tagType==2){
				if(!field.nullAble){
					validOption.editable = false;
					validOption.validType = "comboxRequired['#"+fieldNamePre+field.tagName+"']";
				}
				$("select[name='"+fieldNamePre+field.tagName+"']").combobox(validOption);
			}
		});
	},
	createInitData:function(){
		if(!FormTable.data){
			return "";
		}
		var html = "";
		for(var j in FormTable.data){
			var value = "";
			if(FormTable.data[j]!='null'&&FormTable.data[j]!=null){
				value = FormTable.data[j];
			}
			html += "<input type='hidden' name='orig_"+j+"' value='"+value+"'>";
		}
		return html;
	},
	getformTableData:function(tableId){
		$("#"+tableId)
	}
}
